package com.ptszyxx.popose.module.base.user;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProviders;
import com.luck.picture.lib.entity.LocalMedia;
import com.ptszyxx.popose.BR;
import com.ptszyxx.popose.R;
import com.ptszyxx.popose.databinding.ActivityUserAvatarBinding;
import com.ptszyxx.popose.module.base.user.vm.UserAvatarVM;
import com.ysg.base.BaseFragment;
import com.ysg.http.data.AppViewModelFactory;
import com.ysg.utils.YCollectionUtil;
import com.ysg.widget.pic.YPicUtils;
import java.util.ArrayList;

/**
 * 完善用户信息
 */
public class UserAvatarFragment extends BaseFragment<ActivityUserAvatarBinding, UserAvatarVM> {

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.activity_user_avatar;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public UserAvatarVM initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(UserAvatarVM.class);
    }

    @Override
    public void initData() {
        super.initData();
        viewModel.requestOosToken();
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.uc.onPicEvent.observe(this, o -> addAvatar());
    }

    private void addAvatar() {
        YPicUtils.getInstance().addAvatar(getActivity(), new YPicUtils.OnResultListener() {
            @Override
            public void onResultListener(ArrayList<LocalMedia> result) {
                if (YCollectionUtil.isNotEmpty(result)) {
                    viewModel.avatarPath.set(result.get(0).getCutPath());
                }
            }
        });
    }


}